﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class logn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["se1"] == null)
        {
            Panel2.Visible = false;
            Panel1.Visible = true;
        }

        else
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            loginname.Text = Session["se1"].ToString();
        }
    }
    protected void zhuce_Click(object sender, EventArgs e)
    {
        string name = TextBox1.Text.Trim();
        string pwd1 = TextBox2.Text.Trim();
        string pwd2 = TextBox3.Text.Trim();
        string email = TextBox4.Text.Trim();
        DateTime datetime = DateTime.Now;
        if (new Xqsys.BLL.Users().ExistUserName(name))
        {
            lit.Text = "<script>alert('用户已存在！');</script>";
        }
        else
        {
            if (pwd1 == pwd2)
            {
                string pwd = pwd1;
                Xqsys.MODEL.Users users = new Xqsys.MODEL.Users();
                users.Username = name;
                users.Password = pwd;
                users.Email = email;
                users.RegTime = datetime;
                users.Role = 1;
                bool str = System.Text.RegularExpressions.Regex.IsMatch(email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
                if (str)
                {
                    if (new Xqsys.BLL.Users().AddUser(users))
                        lit.Text = "<script>alert('注册成功！');</script>";
                    else
                        lit.Text = "<script>alert('注册失败');</script>";
                }
                else
                {
                    lit.Text = "<script>alert('请输入合法邮箱');</script>";
                }
            }
            else
            {
                lit.Text = "<script>alert('两次输入密码不一致');</script>";
                TextBox2.Text = "";
                TextBox3.Text = "";
            }
        }
    }
    protected void tijiao_Click(object sender, EventArgs e)
    {
        string name = TextBox5.Text.Trim();
        string pwd = TextBox6.Text.Trim();
        Xqsys.MODEL.Users user=new Xqsys.MODEL.Users();
        user.Username=name;
        if (new Xqsys.DAL.Users().ExistUserName(name))
        {
            DataSet ds = new Xqsys.BLL.Users().Login(user);
            string pwd1 = ds.Tables[0].Rows[0]["password"].ToString();
            if (pwd == pwd1)
            {
                lit.Text = "<script>alert('登录成功');</script>";
                Session["se1"] = ds.Tables[0].Rows[0]["userName"];
                Session["seID"] = ds.Tables[0].Rows[0]["userID"];
                Session["Role"] = ds.Tables[0].Rows[0]["Role"];
                Response.Redirect("~/index.aspx");
            }
            else
            {
                lit.Text = "<script>alert('密码或用户名错误');</script>";
            }
        }
        else
        {
            lit.Text = "<script>alert('密码或用户名错误!');</script>";
        }
    }
    protected void zhuxiao_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
    //protected void TextBox1_TextChanged(object sender, EventArgs e)
    //{
    //    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "test", "alert('run');", true);
    //}
}
